package cn.itheima.controller.role;

import cn.itheima.controller.base.BaseComtroller;
import cn.itheima.domain.Module;
import cn.itheima.domain.Role;
import cn.itheima.service.module.ModuleService;
import cn.itheima.service.role.RoleService;
import cn.itheima.util.UtilFuns;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

@Controller
@RequestMapping(value = "/system/role")
public class RoleController extends BaseComtroller {
    @Autowired
    private RoleService roleService;
    @Autowired
    private ModuleService moduleService;


    @RequestMapping(name = "查询成员",value = "/list")
    public String findAll(@RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "5") int pageSize){

        PageInfo pb = roleService.findAll(pageNum,pageSize,company_id);
        request.setAttribute("pb",pb);

        return "/system/role/role-list";
    }
    @RequestMapping(name = "删除角色",value = "/delete")
    public String delete(String id){
        roleService.delete(id);

        return "redirect:/system/role/list.do";
    }

    @RequestMapping(name = "去添加页面",value = "/toAdd")
    public String  toAdd(String id){


        return "/system/role/role-add";
    }

    @RequestMapping(name = "添加和修改角色",value = "/edit")
    public String  edit(Role role){
        role.setCompanyId(company_id);
        role.setCompanyName(company_name);
        if (UtilFuns.isEmpty(role.getId())){
            String s = UUID.randomUUID().toString();
            role.setId(s);
            roleService.edit(role);
        }else {
            roleService.update(role);
        }
        return "redirect:/system/role/list.do";
    }

    @RequestMapping(name = "返回修改角色的数据",value = "/toUpdate")
    public String  toUpdate(String id){
        Role role = roleService.findById(id);
        request.setAttribute("role",role);

        return "/system/role/role-add";
    }
    @RequestMapping(name = "添加模块",value = "/roleModule")
    public String roleModule(String roleid){
        Role role = roleService.findById(roleid);
        request.setAttribute("role",role);
        return "/system/role/role-module";
    }
    @RequestMapping(name = "查看用户模块",value = "/getZtreeNodes")
    public @ResponseBody List<Map> findRoleModule(String roleid){

        List<Module> checkedModule=moduleService.findRoleModule(roleid);
        List<Module> allModule = moduleService.findAll();
       List<Map> modulelist = new ArrayList<>();
        for (Module module : allModule) {
            HashMap map = new HashMap<>();
           map.put("pId",module.getParentId()) ;
           map.put("id",module.getId()) ;
           map.put("name",module.getName()) ;
            if (checkedModule.contains(module)){
                map.put("checked",true);
            }
            modulelist.add(map);
        }
        return modulelist;
    }

    @RequestMapping(name = "修改用户模块",value = "/updateRoleModule")
    public String updateRoleModule(String roleid,String moduleIds){
        moduleService.updateRoleModule(roleid,moduleIds);
        return "redirect:/system/role/list.do";
    }
}
